<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2015/6/19
 * Time: 16:23
 */

class ExpressAction extends BaseAction
{

    public function index()
    {
        $list = M('Express')->where(array('status'=>1))->select();
        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 设置
     */
    public function set()
    {
        if(IS_POST){
            $id = $this->_post('id',0);
            $title = $this->_post('title');
            $user = $this->_post('user');
            $contact = $this->_post('contact');
            $price = $this->_post('price');
            $data = array();
            if($id){
                //修改
                $data['title'] = $title;
                $data['user'] = $user;
                $data['contact'] = $contact;
                $data['price'] = $price;
                $re = M('Express')->where(array('id'=>$id))->save($data);
                if($re!==false){
                    $this->_success('修改成功');
                }else{
                    $this->_error('修改失败');
                }
            }else{
                //添加
                $data['title'] = $title;
                $data['user'] = $user;
                $data['contact'] = $contact;
                $data['price'] = $price;
                $data['create_time'] = time();
                $InstId = M('Express')->add($data);
                if($InstId){
                    $this->_success('添加成功');
                }else{
                    $this->_error('添加失败');
                }
            }
        }else{
            $this->display();
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = $this->_get('id',0);
        if($id){
            $data = M('Express')->where(array('id'=>$id))->find();
            $this->assign('data',$data);
            $this->display('set');
        }else{
            $this->_error('修改失败');
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = $this->_get('id',0);
        if($id){
            $re = M('Express')->where(array('id'=>$id))->update(array('status'=>0));
            if($re){
                $this->_success('删除成功');
            }else{
                $this->_error('删除失败');
            }
        }
    }
}